package com.yonyou.pmclouds.copyperson.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoInsertMultiProcessor;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoInsertProcessor;
import com.yonyou.pmclouds.basecom.bp.template.MultiInsertBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleInsertBpTemplate;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.copyperson.entity.CopyPersonVO;
import com.yonyou.pmclouds.copyperson.mapper.CopyPersonMapper;
import com.yonyou.pmclouds.copyperson.rmiitf.ICopyPersonMaintain;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = ICopyPersonMaintain.class)
public class CopyPersonMaintainImpl implements ICopyPersonMaintain {

    private final CopyPersonMapper copyPersonMapper;

    @Autowired
    public CopyPersonMaintainImpl(CopyPersonMapper copyPersonMapper) {
        this.copyPersonMapper = copyPersonMapper;
    }


    @Override
    public CopyPersonVO insert(CopyPersonVO copyPersonVO) throws BusinessException {
        SingleInsertBpTemplate<CopyPersonVO> bpTemplate = new SingleInsertBpTemplate<>(copyPersonMapper);
        bpTemplate.addBeforeProcessor(new AuditInfoInsertProcessor<CopyPersonVO>());
        return bpTemplate.insert(copyPersonVO);
    }

    @Override
    public void delete(CopyPersonVO copyPersonVO) throws BusinessException {

    }

    @Override
    public CopyPersonVO update(CopyPersonVO copyPersonVO) throws BusinessException {
        return null;
    }

    @Override
    public CopyPersonVO[] insertBatch(List<CopyPersonVO> copyPersonVOList) throws BusinessException {
        MultiInsertBpTemplate<CopyPersonVO> bpTemplate = new MultiInsertBpTemplate<>(copyPersonMapper);
        bpTemplate.addBeforeProcessor(new AuditInfoInsertMultiProcessor<CopyPersonVO>());
        return bpTemplate.insert(copyPersonVOList.toArray(new CopyPersonVO[]{}));
    }

    @Override
    public void deleteBatchForCopypersion(List<CopyPersonVO> copyPersonVOS,String pkWorkstopreport) throws BusinessException {

        if(null == copyPersonVOS || copyPersonVOS.size() == 0){
            return;
        }

        List<String> copyPersons = new ArrayList<>();

        for(CopyPersonVO copyPersonVO : copyPersonVOS){
            copyPersons.add(copyPersonVO.getCopyPerson());
        }
        copyPersonMapper.deleteByCopyPerson(copyPersons,pkWorkstopreport);
    }
}
